package com.martix.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;

/**
 * <p>支持热更新的简单配置文件<p/>
 * <p>适用于极简项目。甚至是不使用数据库的springboot项目可以用这种方式实现部分需要配置数据的热更新的配置文件。<p/>
 * <p>如果有数据库，可以通过新增配置表来实现热更新。<p/>
 * <p>如果是大型项目可使用 nacos 进行热更新配置<p/>
 *
 * @author liuweizhong
 * @since 2025-09-08 10:26
 */
public class SettingUtil {

    private String settingUrl = "setting.conf";

    private static final String SETTING_READ_FILE_ERROR = "读取配置文件失败";

    private SettingUtil() {
    }


    private static final class InstanceHolder {
        private static final SettingUtil INSTANCE = new SettingUtil();
    }

    /**
     * 获取单例对象
     */
    public static SettingUtil getInstance(String settingUrl) {
        SettingUtil instance = InstanceHolder.INSTANCE;
        instance.setSettingUrl(settingUrl);
        return instance;
    }

    /**
     * 获取单例对象
     */
    public static SettingUtil getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private void setSettingUrl(String settingUrl) {
        this.settingUrl = settingUrl;
    }


    public String getSettingByKey(String key) {
        String setting = "";
        // 读取文件内容到Stream流中，按行读取
        try (
                Stream<String> lines = Files.lines(Paths.get(settingUrl))
        ) {
            AtomicReference<String> value = new AtomicReference<>();
            lines.forEach(line -> {
                if (!line.startsWith("#")) {
                    String[] split = line.split(":");
                    if (split.length == 2) {
                        split[0] = split[0].trim();
                        if (split[0].equals(key)) {
                            value.set(split[1].trim());
                        }
                    }
                }
            });
            setting = value.get().trim();
        } catch (IOException e) {
            throw new RuntimeException(SETTING_READ_FILE_ERROR);
        }
        return setting;
    }

    /**
     * 返回多个配置项的Map
     *
     * @param keys 多个配置项的key
     * @return key - value
     */
    public Map<String, String> getSettingMap(List<String> keys) {
        Map<String, String> map = new HashMap<>();
        // 读取文件内容到Stream流中，按行读取
        try (
                Stream<String> lines = Files.lines(Paths.get(settingUrl))
        ) {
            lines.forEach(line -> {
                if (!line.startsWith("#")) {
                    String[] split = line.split(":");
                    if (split.length == 2) {
                        split[0] = split[0].trim();
                        if (keys.contains(split[0])) {
                            map.put(split[0], split[1].trim());
                        }
                    }
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(SETTING_READ_FILE_ERROR);
        }
        return map;
    }


}
